package action.FrontAction;

import com.opensymphony.xwork2.ActionSupport;
import entity.CroCustomer;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroCustomerService;

import java.util.UUID;

/**
 * Created by xuweijie on 17-5-15.
 */
public class RegisterAction extends ActionSupport {
    @Autowired
    private CroCustomerService croCustomerService;

    private String successResult;
    private String username;
    private String sex;
    private String email;
    private String phone;
    private String password;

    /*
    * 显示注册
    * */
    public String showregister(){
        return SUCCESS;
    }

    /*
    * 添加注册信息
    * */
    public String registerdata(){
        CroCustomer croCustomer=null;
        boolean flag=true;
        croCustomer=croCustomerService.FindCusByEmailOrPhone(email,"1");
        if(croCustomer!=null){
            successResult="0";//邮箱已存在
            flag=false;
        }
        croCustomer=croCustomerService.FindCusByEmailOrPhone(phone,"2");
        if(croCustomer!=null){
            successResult="1";//手机号码已存在
            flag=false;
        }
        if(flag){
           CroCustomer customer=new CroCustomer();
           customer.setUsername(username);
           customer.setSex(sex);
           customer.setEmail(email);
           customer.setPhone(phone);
           String salt= UUID.randomUUID().toString().substring(0,8);
           password=new Md5Hash(password,salt).toString();
           customer.setPasswd(password);
           customer.setSalt(salt);
           customer.setBalance(0);
           if(croCustomerService.insertCustomer(customer)){
               successResult="2";//保存成功
           }else {
               successResult="3";//保存失败
           }
        }
        return SUCCESS;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
